package com.wlc.collection;

import java.util.*;

/**
 * @author 王立朝 wanglichao@champion-credit.com
 * @description Collections 的比较器 Comparable
 * @date 2021-07-13 12:40
 */
public class CompareDemo {

    public static void method01() {

        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(22);
        list.add(32);
        list.add(52);
        list.add(21);
        list.add(24);
        list.add(27);
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        };
        Collections.sort(list, comparator);
        for (int i : list) {
            System.out.print(i + " ");
        }
    }

    /**
     * 通过要比较的对象 实现Comparable<Hero> 接口 ，然后通过调用Collections.sort(list)来进行排序
     */
    public static void method02() {

        List<Hero> heroes = new ArrayList<>();
        heroes.add(new Hero("李派", 23));
        heroes.add(new Hero("李派1", 21));
        heroes.add(new Hero("李派2", 45));
        heroes.add(new Hero("李派3", 28));
        heroes.add(new Hero("李派4", 78));

        Collections.sort(heroes);
        for (Hero hero : heroes) {
            System.out.println("hero->" + hero);
        }
    }

    public static void myTreeSet() {
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        };
        TreeSet<Integer> treeSet = new TreeSet<Integer>(comparator);
        treeSet.add(2);
        treeSet.add(98);
        treeSet.add(6);
        treeSet.add(34);
        treeSet.add(56);
        for (Integer integer : treeSet) {
            System.out.print(integer + " ");
        }



    }


    private transient NavigableMap<?,Object> m;
    public void method03(){

    }


    public static void main(String[] args) {
        myTreeSet();
    }
}
